package ciyuan.Block.Machine.ryfdj;

import ciyuan.Gui.ContainerRegistry;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Utils.TileEntityRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class RyfdjTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private final Inventory inventory = new Inventory(1);
    public ciyuan.Block.Machine.ryfdj.ContainerProgress containerProgress = new ContainerProgress();

    public RyfdjTileEntity() {
        super(TileEntityRegisterManager.ryfdj.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("ciyuan.container.dsc23");
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new RyfdjContainer(ContainerRegistry.ryfdjContainer.get(), sycID, inventory, getBlockPos(), containerProgress);
    }

    @Override
    public void load(BlockState Ryfdjate, CompoundNBT nbt) {
        if (!hasLevel()) {
            this.inventory.setItem(0,ItemStack.of(nbt.getCompound("item")));
            this.containerProgress.set(0, nbt.getInt("water"));
        }
        super.load(Ryfdjate, nbt);
    }

    @Override
    public CompoundNBT save(CompoundNBT compound) {
        ItemStack itemRyfdjack = this.inventory.getItem(0).copy();
        compound.put("item", itemRyfdjack.serializeNBT());
        compound.putInt("water",this.containerProgress.get(0) );
        return super.save(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        if (!this.inventory.getItem(0).isEmpty()) {
             if (this.inventory.getItem(0).getItem().getDescriptionId().equals(ItemRegisterManager.stonepot4.get().getDescriptionId())) {
                 if (this.containerProgress.get(2)>0) {
                     this.containerProgress.set(2,this.containerProgress.get(2)-1);
                     this.containerProgress.set(0,this.containerProgress.get(0)+1);
                     this.containerProgress.set(1,1);
                 } else {
                     this.inventory.getItem(0).setCount(this.inventory.getItem(0).getCount()-1);
                     this.containerProgress.set(2,this.containerProgress.get(2)+1000);
                 }
             }
        } else {
            this.containerProgress.set(1,0);
        }
    }

}
